<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Factory as Storess;
/**
 * 委外工厂接口
 */
class Factory extends Common
{
    protected $noNeedRight = ['getDetail'];

    /**
     * 获取委外工厂
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 委外工厂名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $where = [];
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['customer_name|customer_code'] = ['like','%'.$keyword.'%'];
            }
            $status = $this->request->post('status');
            if($status!=null && $status!==''){
                $where['status'] = $status;
            }

            $result = Storess::getList($where,$page,$limit);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取委外工厂详情
     * @ApiMethod (POST)
     * @param int $id 委外工厂ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = Storess::getDetail($id);
            $this->success('查询成功',$result,200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加委外工厂
     * @ApiMethod (POST)
     * @param array $detail 委外工厂数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Storess::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新委外工厂
     * @ApiMethod (POST)
     * @param array $detail 委外工厂数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Storess::editDetail($data);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除委外工厂
     * @ApiMethod (POST)
     * @param int $id 委外工厂id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Storess::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
